package com.lipeng.api;

import com.lipeng.bean.Game;
import com.lipeng.bean.Master;
import com.lipeng.service.GameService;
import com.lipeng.service.MasterService;
import com.lipeng.utils.AjaxResult;
import com.lipeng.utils.IPUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by dllo on 18/3/16.
 */
@Controller
@ResponseBody
@RequestMapping(value = "/api/")
public class GameControllerAPI {
    @Resource
    private GameService gameService;


    //查看比赛列表
    @RequestMapping(value = "allgame")
    public AjaxResult allMaster(){
        AjaxResult result = new AjaxResult();
        List<Game> gameList = gameService.selectAllGame();
        if (gameList == null){
            result.setCode("0");
            result.setMessage("查询失败");
            return result;
        }
        for (Game game : gameList){
            game.setGpic(IPUtils.getIP()+":8080"+game.getGpic());
        }
        result.setCode("1");
        result.setMessage("查询成功");
        result.setData(gameList);
        return result;
    }

    //比赛详情
    @RequestMapping(value = "gamedetail")
    public AjaxResult gameDetail(@RequestParam("gid")Integer gid){
        AjaxResult result = new AjaxResult();
        Game game = gameService.selectGameById(gid);
        if (game == null){
            result.setCode("0");
            result.setMessage("查询失败");
            return result;
        }

        game.setGpic(IPUtils.getIP()+":8080"+game.getGpic());
        result.setCode("1");
        result.setMessage("查询成功");
        result.setData(game);
        return result;
    }

    //根据名称搜索
    @RequestMapping(value = "searchgamename")
    public AjaxResult searchname(@Param("name")String name){
        AjaxResult result = new AjaxResult();
        List<Game> gameList = gameService.selectGameByName(name);
        if (gameList==null){
            result.setCode("0");
            result.setMessage("无相关信息");
            return result;
        }
        for (Game game : gameList){
            game.setGpic(IPUtils.getIP()+":8080"+game.getGpic());
        }
        result.setCode("1");
        result.setMessage("查询成功");
        result.setData(gameList);
        return result;
    }


    //购买门票
    @RequestMapping(value = "buytickets")
    public AjaxResult buyTickets(@RequestParam("uid")Integer uid,@RequestParam("gid")Integer gid,@RequestParam("count")Integer count){
        return gameService.buyTickets(uid,gid,count);
    }

    //付款
    @RequestMapping(value = "paytickets")
    public AjaxResult payOrder(@RequestParam("tid")Integer tid){
        return gameService.payTickets(tid);
    }

    //取消订单
    @RequestMapping(value = "deltickets")
    public AjaxResult delTickets(@RequestParam("tid")Integer tid){
        return gameService.delTickets(tid);
    }
}
